//
//  DSOViewController.m
//  Balance
//
//  Created by oracle on 7/17/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "DSOViewController.h"
#import "CorePlot-CocoaTouch.h"
#import "ColorUtility.h"
#import "DSODataModel.h"
#import "EskPlotTheme.h"
#import "DSOLinePlot.h"
#import "DSOSliderPlot.h"

@interface DSOViewController ()


@property (strong, nonatomic) IBOutlet CPTGraphHostingView *dsoLineGraphHostView;
@property (strong, nonatomic) IBOutlet CPTGraphHostingView *dsoTimeRangeGraphHostView;
@property (strong, nonatomic) IBOutlet UIView *noteDetailHostView;
@property (strong, nonatomic) IBOutlet UIButton *notesButton;
@property (strong, nonatomic) IBOutlet UIButton *detailsButton;
@property (strong, nonatomic) IBOutlet UIButton *noOfCollectorsButton;
@property (strong, nonatomic) IBOutlet UIButton *bestPossibleDSOButton;
@property (strong, nonatomic) IBOutlet UIButton *priorPeriodButton;
@property (strong, nonatomic) IBOutlet UIButton *dsoForAllButton;
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
 
@property (strong, nonatomic) DSODataModel *dm;


@property (strong, nonatomic) IBOutlet UIPageControl *pageControl;



- (IBAction)noOfCollectorsButtonPressed:(id)sender;

- (IBAction)bestPossibleDSOButtonPressed:(id)sender;

- (IBAction)priorPeriodButtonPressed:(id)sender;

- (IBAction)dsoForAllButtonPressed:(id)sender;

- (IBAction)pageChanged:(id)sender;

@end

@implementation DSOViewController

#define kNumberOfPages 2
#define kCurrentPlot @"SelectedCustomerDSOPlot"
#define kPriorYearPlot @"PriorYearDSOPlot"
#define kDSOForAllPlot @"DSOForAllPlot"
#define kBestPossibleDSOPlot @"BestPossibleDSOPlot"
#define kNoOfCollectorsPlot @"NoOfCollectorsPlot"
#define kRulerPlot @"RulerPlot"


@synthesize dsoLineGraphHostView=_dsoLineGraphHostView;
@synthesize dsoTimeRangeGraphHostView=_dsoTimeRangeGraphHostView;
@synthesize noteDetailHostView=_noteDetailHostView;
@synthesize notesButton=_notesButton;
@synthesize detailsButton=_detailsButton;
@synthesize noOfCollectorsButton=_noOfCollectorsButton;
@synthesize bestPossibleDSOButton=_bestPossibleDSOButton;
@synthesize priorPeriodButton=_priorPeriodButton;
@synthesize dsoForAllButton=_dsoForAllButton;
@synthesize scrollView = _scrollView;
@synthesize pageControl = _pageControl;
@synthesize dm = _dm;






#pragma mark - Setter/Getters

-(void) setDsoForAllButton:(UIButton *)dsoForAllButton
{
    
    CAGradientLayer *gradientLayer = [ColorUtility buttonGradientLayer];
    gradientLayer.frame =dsoForAllButton.layer.bounds;
    
    [dsoForAllButton.layer insertSublayer:gradientLayer atIndex:0];
    dsoForAllButton.titleLabel.textColor = [UIColor whiteColor];
    
    _dsoForAllButton = dsoForAllButton;
    
}

-(void) setPriorPeriodButton:(UIButton *)priorPeriodButton
{
    
    CAGradientLayer *gradientLayer = [ColorUtility buttonGradientLayer];
    gradientLayer.frame =priorPeriodButton.layer.bounds;
    
    [priorPeriodButton.layer insertSublayer:gradientLayer atIndex:0];
    priorPeriodButton.titleLabel.textColor = [UIColor whiteColor];  
    
    _priorPeriodButton = priorPeriodButton;
    
}

-(void) setBestPossibleDSOButton:(UIButton *)bestPossibleDSOButton
{
    
    CAGradientLayer *gradientLayer = [ColorUtility buttonGradientLayer];
    gradientLayer.frame =bestPossibleDSOButton.layer.bounds;
    
    [bestPossibleDSOButton.layer insertSublayer:gradientLayer atIndex:0];
    bestPossibleDSOButton.titleLabel.textColor = [UIColor whiteColor];
    
    _bestPossibleDSOButton = bestPossibleDSOButton;

}
-(void) setNoOfCollectorsButton:(UIButton *)noOfCollectorsButton
{
    CAGradientLayer *gradientLayer = [ColorUtility buttonGradientLayer];
    gradientLayer.frame =noOfCollectorsButton.layer.bounds;
    
    [noOfCollectorsButton.layer insertSublayer:gradientLayer atIndex:0];
    noOfCollectorsButton.titleLabel.textColor = [UIColor whiteColor]; 
    
    _noOfCollectorsButton = noOfCollectorsButton;

}

-(void) setNotesButton:(UIButton *)notesButton
{
    CAGradientLayer *gradientLayer = [ColorUtility buttonGradientLayer];
    gradientLayer.frame =notesButton.layer.bounds;
    
    [notesButton.layer insertSublayer:gradientLayer atIndex:0];
    notesButton.titleLabel.textColor = [UIColor whiteColor]; 
    
    _notesButton = notesButton;

}

-(void) setDetailsButton:(UIButton *)detailsButton
{
    CAGradientLayer *gradientLayer = [ColorUtility buttonGradientLayer];
    gradientLayer.frame =detailsButton.layer.bounds;
    
    [detailsButton.layer insertSublayer:gradientLayer atIndex:0];
    detailsButton.titleLabel.textColor = [UIColor whiteColor]; 
    
    _detailsButton = detailsButton;

}

-(void) setNoteDetailHostView:(UIView *)noteDetailHostView
{
    CAGradientLayer *gradientLayer = [ColorUtility toolBarGradientLayer];
    
    gradientLayer.frame = noteDetailHostView.layer.bounds;
    [noteDetailHostView.layer insertSublayer:gradientLayer atIndex:0];
    
    _noteDetailHostView = noteDetailHostView;
}

#pragma mark - IBAction methods





#pragma mark - UISplitViewControllerDelegate Methods



# pragma  mark - UIScrollViewDelegate Mehods


#pragma mark - UIViewController Life Cycle Methods


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
     
	// Do any additional setup after loading the view.
    
    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * kNumberOfPages, self.scrollView.frame.size.height);
    
    self.scrollView.delegate = self;
    self.view.backgroundColor = [UIColor colorWithWhite:0.7f alpha:1.0f];
    
    
    //initialze the data model
    self.dm = [[DSODataModel alloc] init];
    dmCopy = [[DSODataModel alloc] init];
    
    
    
    //draw the line plots
    EskPlotTheme *defaultTheme = [[EskPlotTheme alloc] init];
    linePlot = [[DSOLinePlot alloc] initWithOneCustomerData:self.dm.dailyDSO allCustomersData:self.dm.allCustomersDSO];
    [linePlot renderInLayer:self.dsoLineGraphHostView withTheme:defaultTheme];
    
    //draw the slider plot
    sliderPlot = [[DSOSliderPlot alloc] initWithOneCustomerData:dmCopy.dailyDSO monthlyData:self.dm.monthlyKeys];
    sliderPlot.delegate = self;
    [sliderPlot renderInLayer:self.dsoTimeRangeGraphHostView withTheme:defaultTheme];
    
    
    dsoForAllPlotAdded = NO;
    priorYearPlotAdded = NO;
    bestPossibleDSOPlotAdded = NO;
    noOfCollectorsPlotAdded = NO;
    
    
//    //prepare the country bar plots
//    NSArray *country = [NSArray arrayWithObjects:@"",@"Brazil",@"China",@"India",@"Russia", nil];
//    NSMutableDictionary *countryCEI = [[NSMutableDictionary alloc] init]; 
//    NSMutableDictionary *allCEI = [[NSMutableDictionary alloc] init]; 
//    
//    CEIData *data = [[CEIData alloc] init];
//    data.ceiDate = [NSDate date];
//    data.ceiPercent = [NSNumber numberWithFloat:(0.0)];
//    data.priorYearPercent = [NSNumber numberWithFloat:(0.0)];
//    data.timeIntervalSinceStart = 0;
//    data.timeIntervalSinceToday = 0;
//    [countryCEI setObject:data forKey:[country objectAtIndex:0]];
//    CEIData *dataAll = [[CEIData alloc] init]; 
//    dataAll.ceiDate = [NSDate date];
//    dataAll.ceiPercent = [NSNumber numberWithFloat:(0.0)];
//    dataAll.priorYearPercent = [NSNumber numberWithFloat:(0.0)];
//    dataAll.timeIntervalSinceStart = 0;
//    dataAll.timeIntervalSinceToday = 0;
//    [allCEI setObject:dataAll forKey:[country objectAtIndex:1]];
//    
//    //data for Brazil
//    CEIData *dataBr = [[CEIData alloc] init];
//    dataBr.ceiDate = [NSDate date];
//    dataBr.ceiPercent = [NSNumber numberWithFloat:(80.0)];
//    dataBr.priorYearPercent = [NSNumber numberWithFloat:(78.0)];
//    dataBr.timeIntervalSinceStart = 0;
//    dataBr.timeIntervalSinceToday = 0;
//    [countryCEI setObject:dataBr forKey:[country objectAtIndex:1]];
//    CEIData *dataBrAll = [[CEIData alloc] init]; 
//    dataBrAll.ceiDate = [NSDate date];
//    dataBrAll.ceiPercent = [NSNumber numberWithFloat:(70.0)];
//    dataBrAll.priorYearPercent = [NSNumber numberWithFloat:(68.0)];
//    dataBrAll.timeIntervalSinceStart = 0;
//    dataBrAll.timeIntervalSinceToday = 0;
//    [allCEI setObject:dataBrAll forKey:[country objectAtIndex:1]];
//    
//    //data for China
//    CEIData *dataCh = [[CEIData alloc] init];
//    dataCh.ceiDate = [NSDate date];
//    dataCh.ceiPercent = [NSNumber numberWithFloat:(63.0)];
//    dataCh.priorYearPercent = [NSNumber numberWithFloat:(68.0)];
//    dataCh.timeIntervalSinceStart = 0;
//    dataCh.timeIntervalSinceToday = 0;
//    [countryCEI setObject:dataCh forKey:[country objectAtIndex:2]];
//    CEIData *dataChAll = [[CEIData alloc] init]; 
//    dataChAll.ceiDate = [NSDate date];
//    dataChAll.ceiPercent = [NSNumber numberWithFloat:(70.0)];
//    dataChAll.priorYearPercent = [NSNumber numberWithFloat:(68.0)];
//    dataChAll.timeIntervalSinceStart = 0;
//    dataChAll.timeIntervalSinceToday = 0;
//    [allCEI setObject:dataChAll forKey:[country objectAtIndex:2]];
//    
//    //data for India
//    CEIData *dataIn = [[CEIData alloc] init];
//    dataIn.ceiDate = [NSDate date];
//    dataIn.ceiPercent = [NSNumber numberWithFloat:(92.0)];
//    dataIn.priorYearPercent = [NSNumber numberWithFloat:(83.0)];
//    dataIn.timeIntervalSinceStart = 0;
//    dataIn.timeIntervalSinceToday = 0;
//    [countryCEI setObject:dataIn forKey:[country objectAtIndex:3]];
//    CEIData *dataInAll = [[CEIData alloc] init]; 
//    dataInAll.ceiDate = [NSDate date];
//    dataInAll.ceiPercent = [NSNumber numberWithFloat:(70.0)];
//    dataInAll.priorYearPercent = [NSNumber numberWithFloat:(68.0)];
//    dataInAll.timeIntervalSinceStart = 0;
//    dataInAll.timeIntervalSinceToday = 0;
//    [allCEI setObject:dataInAll forKey:[country objectAtIndex:3]];
//    
//    //data for Russia
//    CEIData *dataRu = [[CEIData alloc] init];
//    dataRu.ceiDate = [NSDate date];
//    dataRu.ceiPercent = [NSNumber numberWithFloat:(68.0)];
//    dataRu.priorYearPercent = [NSNumber numberWithFloat:(74.0)];
//    dataRu.timeIntervalSinceStart = 0;
//    dataRu.timeIntervalSinceToday = 0;
//    [countryCEI setObject:dataRu forKey:[country objectAtIndex:4]];
//    CEIData *dataRuAll = [[CEIData alloc] init]; 
//    dataRuAll.ceiDate = [NSDate date];
//    dataRuAll.ceiPercent = [NSNumber numberWithFloat:(70.0)];
//    dataRuAll.priorYearPercent = [NSNumber numberWithFloat:(68.0)];
//    dataRuAll.timeIntervalSinceStart = 0;
//    dataRuAll.timeIntervalSinceToday = 0;
//    [allCEI setObject:dataRuAll forKey:[country objectAtIndex:4]];
//    
//    
//    
//    barPlot = [[CEIBarPlot alloc] initWithCountryNames:country andCurrentData:countryCEI andAllCollectorsData:allCEI];        
//    
//    [barPlot renderInLayer:_barHostingView withTheme:defaultTheme];
    self.view.backgroundColor = [UIColor colorWithWhite:0.7f alpha:1.0f];

    
}

- (void)viewDidUnload
{
    [self setDsoLineGraphHostView:nil];
    [self setDsoTimeRangeGraphHostView:nil];
    [self setNoteDetailHostView:nil];
    [self setNotesButton:nil];
    [self setDetailsButton:nil];
    [self setNoOfCollectorsButton:nil];
    [self setBestPossibleDSOButton:nil];
    [self setPriorPeriodButton:nil];
    [self setDsoForAllButton:nil];
    [self setPageControl:nil];
    [self setScrollView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)noOfCollectorsButtonPressed:(id)sender 
{
    if(noOfCollectorsPlotAdded)
    {
        [linePlot removePlot:kNoOfCollectorsPlot];
        noOfCollectorsPlotAdded = NO;
        self.noOfCollectorsButton.titleLabel.textColor = [UIColor whiteColor];
    }
    else 
    {
        [linePlot addPlot:kNoOfCollectorsPlot];
        noOfCollectorsPlotAdded = YES;
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            self.noOfCollectorsButton.highlighted = YES;
            self.noOfCollectorsButton.titleLabel.textColor = [UIColor blueColor];
        }];
    }

}

- (IBAction)bestPossibleDSOButtonPressed:(id)sender 
{
    if(bestPossibleDSOPlotAdded)
    {
        [linePlot removePlot:kBestPossibleDSOPlot];
        bestPossibleDSOPlotAdded = NO;
        self.bestPossibleDSOButton.titleLabel.textColor = [UIColor whiteColor];
    }
    else 
    {
        [linePlot addPlot:kBestPossibleDSOPlot];
        bestPossibleDSOPlotAdded = YES;
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            self.bestPossibleDSOButton.highlighted = YES;
            self.bestPossibleDSOButton.titleLabel.textColor = [UIColor blueColor];
        }];
    }

}

- (IBAction)priorPeriodButtonPressed:(id)sender 
{
    if(priorYearPlotAdded)
    {
        [linePlot removePlot:kPriorYearPlot];
        priorYearPlotAdded = NO;
        self.priorPeriodButton.titleLabel.textColor = [UIColor whiteColor];
    }
    else 
    {
        [linePlot addPlot:kPriorYearPlot];
        priorYearPlotAdded = YES;
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            self.priorPeriodButton.highlighted = YES;
            self.priorPeriodButton.titleLabel.textColor = [UIColor blueColor];
        }];
    }
}

- (IBAction)dsoForAllButtonPressed:(id)sender 
{
    if(dsoForAllPlotAdded)
    {
        [linePlot removePlot:kDSOForAllPlot];
        dsoForAllPlotAdded = NO;
        self.dsoForAllButton.titleLabel.textColor = [UIColor whiteColor];
    }
    else 
    {
        [linePlot addPlot:kDSOForAllPlot];
        dsoForAllPlotAdded = YES;
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            self.dsoForAllButton.highlighted = YES;
            self.dsoForAllButton.titleLabel.textColor = [UIColor blueColor];
        }];
    }

}

- (IBAction)pageChanged:(id)sender 
{
    
}



#pragma mark - DSOSliderPlotDelegate Methods

- (void)sliderPlot:(DSOSliderPlot *)plot fromDate:(NSString *)fromDateString toDate:(NSString *)toDateString{
    
    [linePlot respondToRangeChangeFromDate:fromDateString toDate:toDateString];
    
}



@end
